﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using static CommonMethod.MyConfigSection;

namespace CommonMethod
{
    public static class EmailHelper
    {

        private readonly static string SmtpServer = ReadConfig.Email["BaseInfo"]["SmtpServerAddress"].Value;//smtp服务器
        //private readonly static int SmtpServerPort = Convert.ToInt32(ReadConfig.Email["BaseInfo"]["SmtpServerPort"].Value);
        private readonly static bool SmtpEnableSsl = false;
        private readonly static string SmtpUsername = ReadConfig.Email["BaseInfo"]["EmailSendAddress"].Value;//发件人邮箱地址
        private readonly static string SmtpDisplayName = ReadConfig.Email["BaseInfo"]["SmtpDisplayName"].Value;//发件人昵称
        private readonly static string SmtpUserPassword = ReadConfig.Email["BaseInfo"]["SmtpUserPassword"].Value;//授权码
        private readonly static string[] EmailReceiveAddresses = new string[] { ReadConfig.Email["BaseInfo"]["EmailReceiveAddress"].Value };//收件人
        private readonly static List<string> SimCardExpireCopyToEmail = new List<string>();

        #region 发送邮件到指定收件人
        /// <summary>
        /// 发送邮件到指定收件人
        /// </summary>
        /// <remarks>
        ///  2013-11-18 18:55 Created By iceStone
        /// </remarks>
        /// <param name="subject">主题</param>
        /// <param name="mailBody">正文内容(支持HTML)</param>
        /// <param name="priority">此邮件的优先级</param>
        /// <param name="attachments">附件列表</param>
        /// <returns>是否发送成功</returns>
        /// <exception cref="System.ArgumentNullException">attachments</exception>
        public static bool Send(string subject, string mailBody, MailPriority priority, params Attachment[] attachments)
        {
            foreach (MyKeyValueSetting item in ReadConfig.Email["CopyToEmailList"])
            {
                if (string.IsNullOrEmpty(item.Value))
                {
                    break;
                }
                SimCardExpireCopyToEmail.Add(item.Value);
            }
            if (attachments == null)
                throw new ArgumentNullException("attachments");
            if (EmailReceiveAddresses.Length == 0)
                return false;
            //创建Email实体
            var message = new MailMessage();
            message.From = new MailAddress(SmtpUsername, SmtpDisplayName);
            message.Subject = subject;
            message.Body = mailBody;
            message.BodyEncoding = Encoding.UTF8;
            message.IsBodyHtml = true;
            message.Priority = priority;

            //插入附件
            foreach (var attachent in attachments)
            {
                message.Attachments.Add(attachent);
            }
            //插入收件人地址，抄送地址和密件抄送地址
            foreach (var to in EmailReceiveAddresses.Where(t => !string.IsNullOrEmpty(t)))
            {
                message.To.Add(new MailAddress(to));
            }
            foreach (var cc in SimCardExpireCopyToEmail.Where(c => !string.IsNullOrEmpty(c)))
            {
                message.CC.Add(new MailAddress(cc));
            }
            //foreach (var bcc in bccs.Where(bc => !string.IsNullOrEmpty(bc)))
            //{
            //    message.Bcc.Add(new MailAddress(bcc));
            //}

            //创建Smtp客户端
            var client = new SmtpClient
            {
                Host = SmtpServer,
                Credentials = new NetworkCredential(SmtpUsername, SmtpUserPassword),
                DeliveryMethod = SmtpDeliveryMethod.Network,
                EnableSsl = SmtpEnableSsl,
                //Port = SmtpServerPort
            };
            try
            {
                client.Send(message);
            }
            catch (Exception)
            {
                throw;
                return false;
            }
            //发送邮件
            foreach (var attachent in attachments)
            {
                attachent.Dispose();
            }
            return true;
        }
        #endregion

    }
}
